{% include 'header.html' %}
<p>&nbsp;</p>
<form class="layui-form" id="userform" action="/alert_manage/alert_task/update" method="POST">
{% for key in fields %}
    {% if key == "id" %}
    {% elif  fields[key]["type"] == "text" %}
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">{{ fields[key]["title"] }}</label>
        <div class="layui-input-block">
            <textarea name="{{ key }}" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    {% else %}
    <div class="layui-form-item">
        <label class="layui-form-label">{{ fields[key]["title"] }}</label>
        <div class="layui-input-block">
            <input type="text" name="{{ key }}" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    {% endif %}
{% endfor %}
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
layui.use('form', function(){
  var form = layui.form;

  // 监听提交事件
  form.on('submit(formDemo)', function(data){
    // 阻止表单默认提交行为
    // data.preventDefault(); // 如果使用这行代码也可以阻止默认提交
    var field = data.field; // 获取表单所有字段，包含提交按钮的name值

    // 使用Ajax进行异步提交
    layui.jquery.ajax({
      url: '/alert_manage/alert_task/add', // 服务器接口
      type: 'post',
      contentType: 'application/json',
      data: JSON.stringify(data.field), // 将表单数据转为JSON字符串
      success: function(res){
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
        parent.location.reload();
      }
    });

    // 返回false阻止表单默认提交
    return false;
  });
});
</script>
{% include 'footer.html' %}